package tt.dz.order.pojo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;

import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;

/**
 * @Title: 服务基本信息
 * @author zhoutian
 * @date 2015-11-26
 */
public class OrderServiceInfo {
	// 服务编号:3位服务类型+3位自增数
	private String serviceNo;
	// 父服务编号
	private String parentNo;
	// 父服务名称(非持久化属性)
	private String parentName;
	// 服务名称
	private String name;
	// 针对所有用户是否可用
	private int isCanUse;
	// 是否虚拟服务:1 是，0 否。虚拟服务为了记账方便而设立的
	private int isVirtual;
	// 该服务的虚拟服务编号,如果没有对应的虚拟服务，则为空
	private String virtualNo;
	// 有效标志 1:有效 0：无效
	private Integer isValid;
	// 创建人
	private String creator;
	// 创建时间
	private Date createTime;

	public String getServiceNo() {
		return serviceNo;
	}

	public void setServiceNo(String serviceNo) {
		this.serviceNo = serviceNo;
	}

	public String getParentNo() {
		return parentNo;
	}

	public void setParentNo(String parentNo) {
		this.parentNo = parentNo;
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getIsCanUse() {
		return isCanUse;
	}

	public void setIsCanUse(int isCanUse) {
		this.isCanUse = isCanUse;
	}

	public int getIsVirtual() {
		return isVirtual;
	}

	public void setIsVirtual(int isVirtual) {
		this.isVirtual = isVirtual;
	}

	public String getVirtualNo() {
		return virtualNo;
	}

	public void setVirtualNo(String virtualNo) {
		this.virtualNo = virtualNo;
	}

	public Integer getIsValid() {
		return isValid;
	}

	public void setIsValid(Integer isValid) {
		this.isValid = isValid;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public OrderServiceInfo() {

	}

	private static final String select_all_sql = "select osi1.serviceNo as serviceNo, osi1.parentNo as parentNo, osi2.name as parentName, osi1.name as name, osi1.isCanUse as isCanUse, osi1.isVirtual as isVirtual, osi1.virtualNo as virtualNo, osi1.isValid as isValid, osi1.creator as creator, osi1.createTime as createTime"
			+ " from order_service_info osi1 left join order_service_info osi2 on osi1.parentNo = osi2.serviceNo";

	private static final String update_all_sql = "update order_service_info set serviceNo = :serviceNo, parentNo = :parentNo, name = :name, isCanUse = :isCanUse, isVirtual = :isVirtual, virtualNo = :virtualNo, isValid = :isValid, creator = :creator, createTime = :createTime where serviceNo = :serviceNo";

	public void save() throws BusinessException {
		if (isServiceNameExist("", this.name)) {
			throw new BusinessException("服务名称重复");
		}
		String sql = "insert into order_service_info(serviceNo, parentNo, name, isCanUse, isVirtual, virtualNo, isValid, creator, createTime) values(:serviceNo, :parentNo, :name, :isCanUse, :isVirtual, :virtualNo, :isValid, :creator, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}

	public void update() throws BusinessException {
		if (isServiceNameExist(this.serviceNo, this.name)) {
			throw new BusinessException("服务名称重复");
		}
		BaseDAOUtil.updateObject(update_all_sql, this);
	}

	public void delete() {
		String sql = "delete from order_service_info where serviceNo = :serviceNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", this.serviceNo);
		BaseDAOUtil.delete(sql, paramMap);
	}

	public static boolean isServiceNameExist(String serviceNo, String name) {
		String sql = select_all_sql + " where osi1.serviceNo != :serviceNo and osi1.name = :name";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", serviceNo);
		paramMap.put("name", name);
		List<OrderServiceInfo> list = BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 根据服务编号查询服务基本信息
	 * 
	 * @param serviceNo
	 *            服务编号
	 * @return 服务基本信息
	 */
	public static OrderServiceInfo getById(String serviceNo) {
		String sql = select_all_sql + " where osi1.serviceNo = :serviceNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", serviceNo);
		return BaseDAOUtil.queryForObject(sql, paramMap, OrderServiceInfo.class);
	}

	/**
	 * 根据服务编号查询服务基本信息
	 * 
	 * @return 服务基本信息列表
	 */
	public static List<OrderServiceInfo> getAll() {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return BaseDAOUtil.queryForList(select_all_sql, paramMap, OrderServiceInfo.class);
	}

	/**
	 * 查询所有的一级服务
	 * 
	 * @return 服务基本信息列表
	 */
	public static List<OrderServiceInfo> getAllLevelOne() {
		String sql = select_all_sql + " where osi1.parentNo = '#'";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
	}

	/**
	 * 查询所有有效的一级服务
	 * 
	 * @return 服务基本信息列表
	 */
	public static List<OrderServiceInfo> getAllValidateLevelOne() {
		String sql = select_all_sql + " where osi1.parentNo = '#' and osi1.isvalid = 1";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
	}

	/**
	 * 查询所有的二级服务
	 * 
	 * @return 服务基本信息列表
	 */
	public static List<OrderServiceInfo> getAllLevelTwo() {
		String sql = select_all_sql + " where osi1.parentNo != '#' and osi1.isvalid = 1";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		return BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
	}

	/**
	 * 是否有其他的实体服务关联了此服务
	 * 
	 * @param serviceNo
	 *            服务编号
	 */
	public static boolean isRelatedVirtual(String serviceNo) {
		String sql = select_all_sql + " where osi1.serviceNo != :serviceNo and osi1.virtualNo = :virtualNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", serviceNo);
		paramMap.put("virtualNo", serviceNo);
		List<OrderServiceInfo> list = BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 是否此服务关联了其他虚拟服务
	 * 
	 * @param serviceNo
	 *            服务编号
	 */
	public static boolean hasOtherVirtualService(String serviceNo) {
		String pre_sql = "select osi1.serviceNo as serviceNo, osi1.parentNo as parentNo, osi2.name as parentName, osi1.name as name, osi1.isCanUse as isCanUse, osi1.isVirtual as isVirtual, osi1.virtualNo as virtualNo, osi1.isValid as isValid, osi1.creator as creator, osi1.createTime as createTime"
			+ " from order_service_info osi1 inner join order_service_info osi2 on osi1.parentNo = osi2.serviceNo";
		String sql = pre_sql + " where osi1.serviceNo = :serviceNo and osi1.virtualNo is not null";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("serviceNo", serviceNo);
		List<OrderServiceInfo> list = BaseDAOUtil.queryForList(sql, paramMap, OrderServiceInfo.class);
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	public static String getMaxServiceNoBy(String parentServiceNo) {
		String sql = "select max(cast(serviceNo as numeric)) as max from order_service_info where parentNo = :parentNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("parentNo", parentServiceNo);
		List<Map<String, Object>> serviceNoList = BaseDAOUtil.queryForList(sql, paramMap);
		if (serviceNoList == null || serviceNoList.size() == 0) {
			return parentServiceNo + "000";
		}
		Map<String, Object> resultMap = serviceNoList.get(0);
		Object max = resultMap.get("max");
		if (max == null) {
			return parentServiceNo + "000";
		}
		return max.toString();
	}

	/**
	 * 查询所有服务的服务号以及服务名
	 * 
	 * @author Anson
	 * @createDate 2016年4月30日
	 * @desc 查询所有服务k-v : serviceNo-name<br>
	 *       返回结果为空则返回Empty形式的Map
	 * @return map<serviceNo,name>
	 *
	 */
	public static Map<String, String> getAllServiceCodeAndName() {
		List<OrderServiceInfo> list = getAll();
		Map<String, String> map = new HashMap<String, String>();
		if (!CollectionUtils.isEmpty(list)) {
			for (OrderServiceInfo info : list) {
				map.put(info.getServiceNo(), info.getName());
			}
		}
		return map;
	}
}
